
import { _decorator, Component, Node } from 'cc';
import { Prefab } from 'cc';
import dynamicScrollViewDataV2 from '../../../../scripts/libs/utils/DynmicScrollView/dynamicScrollViewDataV2';
import { pfm } from '../../../../scripts/managers/PlayFabManager';
import { gm } from '../../../../scripts/managers/GameManager';
import { LeaderboardPage } from '../LobbyPanel/LeaderboardPage/LeaderboardPage';
const { ccclass, property } = _decorator;

@ccclass('LeaderboardScrollView')
export class LeaderboardScrollView extends dynamicScrollViewDataV2 {

    leaderboardPage: LeaderboardPage = null;

    @property(Prefab)
    itemPrefab: Prefab = null;

    getPrefab() {
        return this.itemPrefab;
    }

    fetchDataForKey(key: string, skip: number, count: number, addDataFunc: Function): void {
        this.log('fetchDataForKey', key, skip, count);

        let list = [];
        pfm.getLeaderboard('stageNum_manual', skip + 1, count).subscribe(_ => {
            if (_?.[0]?.code == 200) {
                let rankings = _?.[0]?.data?.Rankings;
                let entryCount = _?.[0]?.data?.EntryCount;
                for (let i = 0; i < rankings.length; i++) {
                    let ranking = rankings[i];
                    // let _isLast = false;
                    // if (i == rankings.length - 1 && entryCount < count) {
                    //     _isLast = true;
                    // }
                    list.push({ rank: ranking.Rank - 1, ranking });

                    if (ranking.Entity.Id == pfm.entityId.value) {
                        pfm.myRanking.value = ranking;
                    }
                }

                if (!this.leaderboardPage?.scrollViewReady.value) {
                    if (rankings.length > 0) {
                        this.leaderboardPage.scrollViewReady.value = true;
                    }
                }
            }
            addDataFunc(list);
        });

    }

}